package com.example.foodisimportant.StudentViewActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.foodisimportant.Adapter.SVAdapter;
import com.example.foodisimportant.PersonalInformationActivity.PersonalInformationActivity;
import com.example.foodisimportant.R;
import com.example.foodisimportant.StudentViewActivity.Fragment.SettingsFragment;

import java.util.ArrayList;
import java.util.List;

public class StudentViewActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener {

    //底部菜单4个LinearLayout
    private LinearLayout mLlHome,mLlView,mLlMessage,mLlSettings;
    //底部菜单4个标题
    private TextView mTvHome,mTvView,mTvMessage,mTvSettings;
    //底部菜单四个图片
    private ImageView mIvHome,mIvView,mIvMessage,mIvSettings;
    private String number,password;
    //中间内容区域
    private ViewPager mVp;
    //viewpager适配器ContentAdapter
    private SVAdapter mSa;
    private LinearLayout[] Ll;
    private int[] linearlayout=new int[]{R.id.Ll_1,R.id.Ll_2,R.id.Ll_3,R.id.Ll_4};
    private List<View> views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_view);
        //初始化控件
        initView();
        //初始化底部按钮事件
        initEvent();
    }

    private void initView(){
        //底部四个LinearLayout
        this.mLlHome=findViewById(R.id.Ll_1);
        this.mLlView=findViewById(R.id.Ll_2);
        this.mLlMessage=findViewById(R.id.Ll_3);
        this.mLlSettings=findViewById(R.id.Ll_4);
        //底部四个TextView
        this.mTvHome=findViewById(R.id.home_tv);
        this.mTvView=findViewById(R.id.view_tv);
        this.mTvMessage=findViewById(R.id.message_tv);
        this.mTvSettings=findViewById(R.id.settings_tv);
        //底部四个ImageView
        this.mIvHome=findViewById(R.id.home_image);
        this.mIvView=findViewById(R.id.view_image);
        this.mIvMessage=findViewById(R.id.message_image);
        this.mIvSettings=findViewById(R.id.setting_image);
        //中间内容的适配器
        this.mVp=findViewById(R.id.content);
        //页面适配器
        View page_01 = View.inflate(StudentViewActivity.this, R.layout.page_home, null);
        View page_02 = View.inflate(StudentViewActivity.this, R.layout.page_view, null);
        View page_03 = View.inflate(StudentViewActivity.this, R.layout.page_message, null);
        View page_04 = View.inflate(StudentViewActivity.this, R.layout.page_settings, null);
        //初始颜色
        mTvHome.setTextColor(0xff9839D8);
        mIvHome.setImageResource(R.drawable.home2);

        views = new ArrayList<View>();
        views.add(page_01);
        views.add(page_02);
        views.add(page_03);
        views.add(page_04);

        this.mSa = new SVAdapter(views);
        mVp.setAdapter(mSa);
        number=this.getIntent().getStringExtra("number");
        password=this.getIntent().getStringExtra("password");
        SettingsFragment fragment = new SettingsFragment();
    }

    private void initEvent(){
        //设置按钮监听
        mLlHome.setOnClickListener(this);
        mLlView.setOnClickListener(this);
        mLlMessage.setOnClickListener(this);
        mLlSettings.setOnClickListener(this);
        //设置滑动监听
        mVp.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        //在每次点击之后将底部按钮颜色改为灰
        restartBotton();
        switch (v.getId()){
            case R.id.Ll_1:
                mTvHome.setTextColor(0xff9839D8);
                mIvHome.setImageResource(R.drawable.home);
                mVp.setCurrentItem(0);
                break;
            case R.id.Ll_2:
                mTvView.setTextColor(0xff9839D8);
                mIvView.setImageResource(R.drawable.content);
                mVp.setCurrentItem(1);
                break;
            case R.id.Ll_3:
                mTvMessage.setTextColor(0xff9839D8);
                mIvMessage.setImageResource(R.drawable.message);
                mVp.setCurrentItem(2);
                break;
            case R.id.Ll_4:
                mTvSettings.setTextColor(0xff9839D8);
                mIvSettings.setImageResource(R.drawable.personal);
                mVp.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    private void restartBotton(){
        //TextView设置成灰色
        mTvHome.setTextColor(0xff82858b);
        mTvView.setTextColor(0xff82858b);
        mTvMessage.setTextColor(0xff82858b);
        mTvSettings.setTextColor(0xff82858b);
        //按钮设置成灰色
        mIvHome.setImageResource(R.drawable.home);
        mIvView.setImageResource(R.drawable.content);
        mIvMessage.setImageResource(R.drawable.message);
        mIvSettings.setImageResource(R.drawable.personal);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        restartBotton();
        //当前view被选择的时候,改变底部菜单图片，文字颜色
        switch (position) {
            case 0:
                mTvHome.setTextColor(0xff9839D8);
                mIvHome.setImageResource(R.drawable.home2);
                break;
            case 1:
                mTvView.setTextColor(0xff9839D8);
                mIvView.setImageResource(R.drawable.content2);
                break;
            case 2:
                mTvMessage.setTextColor(0xff9839D8);
                mIvMessage.setImageResource(R.drawable.message2);
                break;
            case 3:
                mTvSettings.setTextColor(0xff9839D8);
                mIvSettings.setImageResource(R.drawable.personal2);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}




